 /*
 * @description: 首页装修
 * @Author: along
 * @Date: 2021-06-11
 * @Last Modified by: along
 * @Last Modified time:
 */
<template>
    <div class="container activeHome">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">模版名称:</p>
                    <el-input
                        v-model="formData.keyword"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="$refs.activeCreatePageDialog.showDialog('add')"
                    >
                        新建装修页
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="activeHome"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="title"
                label="首页名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            />
            <el-table-column
                prop="update_time"
                label="上次修改时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.update_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="上架状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="handleUnStatus(scope.row)"
                        :disabled="!!scope.row.default"
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="start_time"
                label="展示时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="200"
            >
                <template slot-scope="scope">
                    <div class="__flex">
                        <template v-if="(scope.row.start_time === -1 && scope.row.end_time === -1)">
                            长期有效
                        </template>
                        <template v-else>
                            <p>开始时间:{{ scope.row.start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</p>
                            <p>截止时间:{{ scope.row.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</p>
                        </template>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="创建时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="min_app_version"
                label="适用版本"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    <!-- <p
                        class="cursor-font"
                        @click="$refs.activeChangeVersion.showDialog(scope.row)"
                    >
                        {{ scope.row.min_app_version }}
                    </p> -->
                    {{ scope.row.min_app_version }}
                </template>
            </el-table-column>
            <el-table-column
                prop="valid_status"
                label="模版状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    {{ scope.row.valid_status ? '已生效' : '未生效' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="default"
                label="默认配置"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    {{ scope.row.default ? '是' : '否' }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
                :width="190"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEditPage(scope.row)"
                    >
                        装修页面
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="$refs.activeCreatePageDialog.showDialog('edit', scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDeletePage(scope.row)"
                        v-if="!scope.row.default"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 新建首页 -->
        <active-create-page-dialog
            ref="activeCreatePageDialog"
            @create="create"
        />

        <!-- 选择版本 -->
        <active-change-version
            ref="activeChangeVersion"
            @change="handleChangeversion"
        />
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import activeCreatePageDialog from '@/components/active-home/active-create-page-dialog';
import activeChangeVersion from '@/components/active-home/active-change-version';
export default {
    mixins: [back],
    components: {
        activeCreatePageDialog,
        activeChangeVersion
    },
    data () {
        return {
            formData: {
                keyword: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '商城'},{title: '首页模块'},{title: '装修模版'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/custom_page_for_shop_index%5Cget_list', {
                page_id: page || this.page.now_page,
                title: this.formData.keyword
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 装修页面
         */
        fnEditPage (row) {
            // if(!this.authResult.auth_edit) {
            //     this.$notify({
            //         title: '提示',
            //         message: '此权限仅限超级管理员',
            //         type: 'warning',
            //         duration: 2000
            //     });
            //     return;
            // }
            this.$router.push({
                name: 'activeTemplateHome',
                query: {
                    page_no: row.page_no,
                    default: row.default
                }
            });
        },

        /**
         * @description 新建装修页面
         */
        newPage () {
            // if(!this.authResult.auth_add) {
            //     this.$notify({
            //         title: '提示',
            //         message: '此权限仅限超级管理员',
            //         type: 'warning',
            //         duration: 2000
            //     });
            //     return;
            // }
            this.$router.push({
                name: 'activeTemplateHome',
                query: {
                    page_no: ''
                }
            });
        },

        /**
         * @description 上架/下架
         *     同个时间段内只可开启上架一个首页装修模版
         */
        handleUnStatus (row) {
            this.$post('/student/custom_page_for_shop_index%5Cset_status',{
                page_no: row.page_no,
                status: row.status
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success',
                        duration: 2000
                    });
                } else if (resp.code == 0) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 删除
         */
        fnDeletePage (row) {
            this.$confirm('删除后,用户端将不再展示该装修模版,确认删除该项吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/custom_page_for_shop_index%5Cdelete',{
                    page_no: row.page_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * @description 新建首页
         */
        create (info = { type: '', page_no: '', template_name: '', template_time: [], texmlate_lang_time: '' }) {

            console.log('@@@info@@@', info);

            const {
                page_no,
                template_name,
                template_time,
                texmlate_lang_time
            } = info;

            if(info.type == 'add') {
                this.$post('/student/custom_page_for_shop_index%5Cadd',{
                    title: template_name,
                    start_time: texmlate_lang_time ? -1 : (parseInt(new Date(template_time[0]).getTime() / 1000)),
                    end_time: texmlate_lang_time ? -1 : (parseInt(new Date(template_time[1]).getTime() / 1000)),
                    page_config_json: null
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '新建成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else if (resp.code == 0) {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            }

            if(info.type == 'edit') {
                this.getPageConfig(page_no).then(next => {
                    this.$post('/student/custom_page_for_shop_index%5Cset',{
                        page_no: page_no,
                        title: template_name,
                        start_time: texmlate_lang_time ? -1 : (parseInt(new Date(template_time[0]).getTime() / 1000)),
                        end_time: texmlate_lang_time ? -1 : (parseInt(new Date(template_time[1]).getTime() / 1000)),
                        page_config_json: JSON.stringify(next.page_config)
                    },resp=>{
                        if (resp.code == 1) {
                            this.getTableData();
                            this.$notify({
                                title: '提示',
                                message: '编辑成功',
                                type: 'success',
                                duration: 2000
                            });
                        } else if (resp.code == 0) {
                            this.$notify({
                                title: '提示',
                                message: resp.msg,
                                type: 'warning',
                                duration: 2000
                            });
                        }
                    });
                });
            }
        },

        /**
         * @description 获取页面详情数据
         */
        getPageConfig (page_no) {
            return new Promise((resolve,reject) => {
                this.$post('/student/custom_page_for_shop_index%5Cget',{
                    page_no: page_no
                },resp=>{
                    if (resp.code == 1) {
                        resolve(resp.data);
                    }
                });
            });
        },

        /**
         * @description 确定修改版本
         */
        handleChangeversion ( info = { version: '', page_no: '' } ) {
            this.$post('',{
                //
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '修改成功',
                        type: 'success',
                        duration: 2000
                    });
                } else if (resp.code == 0) {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 0px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        margin-bottom: 0px !important;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .__flex {
            display: flex;
            flex-direction: column;
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .activeHome{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
    }
</style>